package com.sgb.kjwl.view.ui.activity.manage;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.androidkun.xtablayout.XTabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCompanyTrendsBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageModule;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.manage.CompanyTradesListEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.manage.CompanyTradesAdapter;
import com.sgb.kjwl.view.widget.ActionBar;
import com.sgb.kjwl.view.widget.EnterprisePopWindow;
import com.sgb.kjwl.view.widget.IpPopWindow;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.viewmodel.manage.CompanyTradesModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

public class CompanyTradesActivity extends AppActivity {

    private ActivityCompanyTrendsBinding binding;

    @Inject
    CompanyTradesModel tradesModel;

    private boolean isLoadMore = false;

    CompanyTradesAdapter companyTradesAdapter;
    private int companyPageNumber;
    private String cityNo = "",mCityName = "", industryNameNo, provinceNo = "",dynamicTypeNo,name;
    private EnterprisePopWindow brandPopWindow;
    private ThreeLevelAreaWindow cityPopWindow2;
    private boolean isChange1 = false, isChange3 = false;
    private IpPopWindow homeGoodsSelectionTypePopWindow;
    private List<IpPickSelectEntity> data;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseManageComponent.builder()
                .appComponent(appComponent)
                .enterpriseManageModule(new EnterpriseManageModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                name = mActionBar.getEditText();
                setDataLoadMore(true);
            }
        });

        mActionBar.setClearEdiText(new ActionBar.ClearEdiText() {
            @Override
            public void clearEdiText() {
                name = "";
                setDataLoadMore(true);
            }

            @Override
            public void search() {
                closeKeybord();
                name = mActionBar.getEditText();
                setDataLoadMore(true);
            }
        });
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_company_trends);

        setDataLoadMore(true);

        homeGoodsSelectionTypePopWindow = new IpPopWindow(this);
        homeGoodsSelectionTypePopWindow.setBgView(binding.grayLayout);
        homeGoodsSelectionTypePopWindow.setDefItem(0);

        homeGoodsSelectionTypePopWindow.setOnResultClick((key1, key2, key3) -> {
            binding.tlCompanyTrades.getTabAt(((int) key2)).select();
            binding.editView.setImageResource(R.mipmap.btn_expand1);
        });
        homeGoodsSelectionTypePopWindow.setOnDismissListener(() -> binding.editView.setImageResource(R.mipmap.btn_expand1));

        binding.setClick(tradesModel);
        tradesModel.getCityData();
        brandPopWindow = new EnterprisePopWindow(this);
        brandPopWindow.setBgView(binding.grayLayout);
        brandPopWindow.setOnResultClick((type, key2, key3) -> {
            dynamicTypeNo = (String) type;
            isLoadMore = false;
            String name = (String) key2;
            if (!name.equals("不限")) {
                isChange1 = true;
                setTextViewBg(binding.tvType, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange1 = false;
                setTextViewBg(binding.tvType, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
            }
            setDataLoadMore(true);
        });
        brandPopWindow.setOnDismissListener(() -> {
            if (!isChange1) {
                setTextViewBg(binding.tvType, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
            }
        });

        cityPopWindow2 = new ThreeLevelAreaWindow(this){
            @Override
            protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
                super.provinceClick(baseAreaEntity, position);
                if (!provinceNo.equals(baseAreaEntity.getId())){
                    cityNo = "";
                }

                mCityName = baseAreaEntity.getSimpleName();
                provinceNo = baseAreaEntity.getId();
            }

            @Override
            protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
                super.cityClick(baseAreaEntity, position);
                cityNo = baseAreaEntity.getId();
                mCityName = baseAreaEntity.getSimpleName();
            }

            @Override
            protected void reset() {
                super.reset();
                cityPopWindow2.dismiss();
                setDataLoadMore(true);
            }
        };
        cityPopWindow2.setBgView(binding.grayLayout);
        cityPopWindow2.setShowSimpleName(true);
        cityPopWindow2.setThreeListIsGone(true);
        cityPopWindow2.setProvinceSingle();
        cityPopWindow2.setCitySingle();
        cityPopWindow2.setOnResultClick((key1, key2, key3) -> {
            isLoadMore = false;

            setDataLoadMore(true);
        });
        cityPopWindow2.setOnDismissListener(() -> {
            if (!mCityName.equals("") && !mCityName.equals("不限")) {
                isChange3 = true;
                binding.tvArea.contentSelect(true,false);
            } else {
                isChange3 = false;
                binding.tvArea.contentSelect(false,false);
            }

            if (!isChange3) {
                binding.tvArea.contentSelect(false,false);
            }
        });

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                setDataLoadMore(true);
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                setDataLoadMore(false);
            }
        });

        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        companyTradesAdapter = new CompanyTradesAdapter(this, null);
        binding.recyclerView.setAdapter(companyTradesAdapter);

        tradesModel.clickIndustry(false);

        binding.tvType.setOnClickListener(this);
        binding.tvArea.setOnClickListener(this);
        binding.editView.setOnClickListener(this);

        binding.tlCompanyTrades.addOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                if (homeGoodsSelectionTypePopWindow == null) {
                    homeGoodsSelectionTypePopWindow = new IpPopWindow(CompanyTradesActivity.this);
                }
                homeGoodsSelectionTypePopWindow.setDefItem(tab.getPosition());
                industryNameNo = data.get(tab.getPosition()).getValue();
                setDataLoadMore(true);
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_type:
                setTextViewBg(binding.tvType, R.drawable.search_bg, R.mipmap.icon_up_red, R.color.color_main);
                tradesModel.dynamicList();
                break;
            case R.id.tv_area:
                binding.tvArea.contentSelect(false,true);
                showProvincePopWindow();
                break;
            case R.id.edit_view:
                tradesModel.clickIndustry(true);
                binding.editView.setImageResource(R.mipmap.btn_expand_up_gray);
                break;
        }
    }

    private void setDataLoadMore(boolean isRefresh) {
        if (isRefresh) {
            isLoadMore = false;
            companyPageNumber = 1;
        } else {
            isLoadMore = true;
            companyPageNumber++;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("cityNo", cityNo);
        map.put("dynamicTypeNo", dynamicTypeNo);
        map.put("industryNameNo", industryNameNo);
        map.put("title",name);
        map.put("provinceNo", provinceNo);
        map.put("page", companyPageNumber);
        map.put("limit", 20);
        tradesModel.getCompanyTradesList(map);
    }


    public void getCompanyTradesList(List<CompanyTradesListEntity> list) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                companyTradesAdapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                companyTradesAdapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                companyTradesAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
//                binding.emptyView.setVisibility(View.VISIBLE);
//                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void showBrandPopWindow(List<IpPickSelectEntity> oneData) {
        brandPopWindow.showPopwindow(binding.llTop, 0, 0);
        List<PickSelectEntity> list = new ArrayList<>();
        for (IpPickSelectEntity oneDatum : oneData) {
            PickSelectEntity entity2 = new PickSelectEntity(oneDatum.getValue(), oneDatum.getName());
            list.add(entity2);
        }
        brandPopWindow.setData(list);
    }

    private void showProvincePopWindow() {
        cityPopWindow2.showPopwindow(binding.llTop,0,0);
    }


    private void setTextViewBg(TextView textView, int bg, int img, int textColor) {
        textView.setBackground(getResources().getDrawable(bg));
        Drawable drawable_n = getResources().getDrawable(img);
        drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable_n, null);
        textView.setTextColor(getResources().getColor(textColor));
    }

    public void tabData(List<IpPickSelectEntity> oneData, boolean isList) {
        data = oneData;
        if (!isList) {
            for (IpPickSelectEntity oneDatum : oneData) {
                binding.tlCompanyTrades.addTab(binding.tlCompanyTrades.newTab().setText(oneDatum.getName()));
            }
        } else {
            homeGoodsSelectionTypePopWindow.showPopwindow(binding.tvTag, 0,0);
            homeGoodsSelectionTypePopWindow.setData(oneData);
        }
    }

    public void setCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas, String areaCode) {
        cityPopWindow2.setDataForCompany(datas, true);
    }
}
